package com.bj58.migrate.condition.impl;

import java.util.ArrayList;
import java.util.List;

import com.bj58.migrate.condition.CycleCondtion;

public abstract class AbstractCondition implements CycleCondtion{

    protected List< String > conlist = new ArrayList< String >();

    protected int index = 0;

    protected boolean isFirst = false;
    protected boolean notifyend = false;

    protected CycleCondtion next;
    protected CycleCondtion prev;

    public abstract void intConditionList();

    public int getCount(){

        return conlist.size();
    }

    public boolean next(){

        index++;
        if( index < conlist.size() ){
            if( notifyend && this.prev != null ){
                this.notifyend = false;
                this.prev.origin();
            }
            return true;
        }
        if( next != null ){
            next.notifyEnd();
            return next.next();
        }
        return false;
    }

    public void origin(){

        index = 0;
        this.notifyend = false;
        if( this.prev != null ){
            this.prev.origin();
        }
    }

    public void notifyEnd(){

        notifyend = true;
    }

    ;

    public String getValue(){

        String v = getCurrentValue();

        if( v != null ){
            return ( isFirst ? "" : "&" ) + v + ( next != null ? next.getValue() : "" );
        }

        return "";
    }

    public String getCurrentValue(){

        if( index > -1 && index < conlist.size() ){
            return conlist.get( index );
        }
        return null;
    }

    public void setFirst(){

        this.index = -1;
        this.isFirst = true;
    }

    public void setNextCondtion( CycleCondtion next ){

        this.next = next;
    }

    public void setPrevCondtion( CycleCondtion prev ){

        this.prev = prev;
    }

    public CycleCondtion getNextCondtion(){

        return this.next;
    }

    public CycleCondtion getPrevCondtion(){

        return this.prev;
    }
}
